import {
    Component, ComponentFactoryResolver, ElementRef, ViewContainerRef, Renderer2, OnInit
} from '@angular/core';
import { DrawService } from '../../../common/draw/draw.service';
import { PsMachineDetailComponent } from '../../../detail/psMachine/psMachine.detail.component';
import { EsCommonService } from '../../../common/escommon.service';
import { setInterval } from 'core-js/library/web/timers';
import { TenantsService } from '../../../common/tenants.service';

@Component({
    templateUrl: './tenants.component.html',
    styleUrls: ['./tenants.component.css', '../../../app.component.css'],
})
export class TenantsComponent implements OnInit {
    searchStr: any;
    nowPage: number;
    totalCount: number;
    pageSize;
    basic;
    headers = [
        {
            field: 'name',
            title: '名称',
            canSort: false,
            width: '20%',
        },
        {
            field: 'cpu',
            title: '计算资源',
            canSort: true,
            width: '20%',
        },
        {
            field: 'network',
            title: '网络资源',
            canSort: true,
            width: '20%',
        },
        {
            field: 'disk',
            title: '存储资源',
            canSort: true,
            width: '20%',
        },
        {
            field: 'machine',
            title: '已分配主机数',
            canSort: true,
            width: '20%',
        }
    ];
    datas: Array<any>;
    constructor(private drawService: DrawService, private elementref: ElementRef, private renderer: Renderer2, private esCommonService: EsCommonService,
        private viewContainerRef: ViewContainerRef, private resolver: ComponentFactoryResolver, private tenantsService: TenantsService) {
        this.basic = false;
        this.pageSize = 15;
    }

    ngOnInit(): void {
        this.loadData({
            pageNo: 1,
            pageSize: this.pageSize,
        });
    }

    loadData(data?) {
        let d = this.tenantsService.queryMachine(data);
        this.datas = d.data;
        this.totalCount = d.totalCount;
        //this.totalCount = 100;
        this.nowPage = 1;
    }

    search(event?) {
        if (event == null) {
            this.loadData({
                search: this.searchStr,
                pageNo: 1,
                pageSize: this.pageSize,
            });
        } else {
            if (event.key == 'Enter') {
                this.loadData({
                    search: this.searchStr,
                    pageNo: 1,
                    pageSize: this.pageSize,
                });
            }
        }
    }

    paginationChange() {
        this.loadData({
            search: this.searchStr,
            pageNo: this.nowPage,
            pageSize: this.pageSize,
        });
    }

    getStatus(status) {
        return this.esCommonService.getStatus(status);
    }

    getDate(date) {
        if (date == null) {
            return '-';
        }
        let dateObject = new Date(date);

        return this.esCommonService.getFormatDate(dateObject, "yyyy-MM-dd hh:mm:ss")
    }

    sortEvent(event) {
        this.loadData({
            search: this.searchStr,
            pageNo: 1,
            pageSize: this.pageSize,
            sort: event
        });
    }

    openSlider(psMachine) {
        this.drawService.open({
            component: PsMachineDetailComponent,
            data: {
                data: psMachine,
                type: 'tenants'
            }
        });
    }

    downloadPdf() {
        this.esCommonService.downloadPdf("pdf");
    }

    jumpToWarn(machine) {
        debugger;
    }

    isOver(data) {
        if (data == null) {
            return false;
        }

        let ds = data.split('%');
        if (ds && ds.length > 0) {
            if (ds[0] >= 80) {
                return true;
            }
        } else {
            return false;
        }
    }
}